﻿@using Jx.Toolbox.Extensions
@using JxAudio.Front.Data
@using JxAudio.TransVo
@using Microsoft.Extensions.Localization

@inject IDispatchService<AddTrackMessage> DispatchService
@inject IStringLocalizer<TrackTable> StringLocalizer

<Table TItem="TrackVo" Items="Tracks" IsBordered="true" IsStriped="true" ShowExtendButtons="true" IsMultipleSelect="true"
       ShowExtendEditButton="false" ShowExtendDeleteButton="false" ShowToolbar="ShowDefaultToolbar" ShowDefaultButtons="false"
       SelectedRows="_selectedRows" SelectedRowsChanged="SelectedChanged" IsKeepSelectedRows="IsKeepSelectedRows"
       ModelEqualityComparer="(vo, trackVo) => vo.Id == trackVo.Id">
    <TableColumns>
        <TableColumn @bind-Field="@context.Title"></TableColumn>
        <TableColumn @bind-Field="@context.Artists">
            <Template Context="value">
                @if (value.Value != null)
                {
                    @foreach (var artist in value.Value)
                    {
                        <LinkButton Url="@($"/Artist/{artist.Id}")">@artist.Name</LinkButton>
                        @:&nbsp;
                    }
                }
            </Template>
        </TableColumn>
        <TableColumn @bind-Field="@context.Album">
            <Template Context="value">
                @if (value.Value != null)
                {
                    <LinkButton Url="@($"/Album/{value.Value.Id}")">@value.Value.Title</LinkButton>
                }
                else
                {
                    @StringLocalizer["UnknownAlbum"]
                }
            </Template>
        </TableColumn>
        <TableColumn @bind-Field="@context.Name" Text="@StringLocalizer["TrackType"]">
            <Template Context="value">
                @Path.GetExtension(value.Value!)[1..].ToUpper()
            </Template>
        </TableColumn>
        <TableColumn @bind-Field="@context.BitRate"></TableColumn>
        <TableColumn @bind-Field="@context.Size">
            <Template Context="value">
                @value.Value.ToSizeString()
            </Template>
        </TableColumn>
        <TableColumn @bind-Field="@context.Duration">
            <Template Context="value">
                @value.Value.ToTimeString()
            </Template>
        </TableColumn>
    </TableColumns>
    <RowButtonTemplate>
        <TableCellButton Icon="fa fa-play" OnClick="() => Play(context)" IsShow="ShowDefaultButton"></TableCellButton>
        <TableCellButton Icon="fa fa-plus" OnClick="() => AddToList(context)" IsShow="ShowDefaultButton"></TableCellButton>
        @TrackButton?.Invoke(context)
    </RowButtonTemplate>
    <TableToolbarTemplate>
        <TableToolbarButton Text="@StringLocalizer["PlayAll"]" Icon="fa fa-play" OnClick="PlayAll" IsShow="ShowDefaultToolbarButton"></TableToolbarButton>
        <TableToolbarButton Text="@StringLocalizer["PlaySelect"]" Icon="fa fa-play-circle" OnClick="PlaySelect" IsShow="ShowDefaultToolbarButton"></TableToolbarButton>
        @ToolbarButtons
    </TableToolbarTemplate>
</Table>